<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { queryLeft1 } from '@/api/business/largeScreen'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '430px'
    }
  },
  data() {
    return {
      chart: null,
      dataAll: [389, 259, 262, 324, 232, 176, 196, 214, 133, 370],
      yAxisData: ['原因1', '原因2', '原因3', '原因4', '原因5']
    }
  },
  mounted() {
    this.initChart('03')
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(zhlb) {
      this.chart = echarts.init(this.$el, 'macarons')

      queryLeft1({ zhlb: zhlb }).then(res => {
        console.log('queryLeft1:{}', res)
        this.chart.setOption({
          title: [
            { text: '每月各区申请人数', textStyle: { color: '#fff', fontSize: '14' }, padding: [
              3, // 上
              10, // 右
              5, // 下
              10 // 左
            ] }
          ],
          grid: {
            x: '50%', y: '7%',
            //    top: '2%',
            left: '2%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          calculable: true,
          xAxis: [
            {
              type: 'value',
              boundaryGap: [0, 0.01],
              axisLabel: {
                textStyle: {
                  color: '#fff',
                  fontSize: 12,
                  textShadowColor: '#000',
                  textShadowOffsetY: 2
                }
              },
              axisLine: {
                lineStyle: {
                  color: '#DDD'
                }
              },
              splitLine: {
                lineStyle: {
                  type: 'dashed',
                  color: '#DDD'
                }
              }
            }
          ],
          yAxis: [
            {
              type: 'category', // y轴
              data: res.timeArray,
              axisLabel: {
                textStyle: {
                  color: '#fff',
                  fontSize: 12,
                  textShadowColor: '#000',
                  textShadowOffsetY: 2
                }
              },
              axisLine: {
                lineStyle: {
                  color: '#DDD'
                }
              }
            }
          ],
          series: [
            {
              name: '南关',
              type: 'bar',
              color: '#86c9f4',
              label: {
                show: true,
                position: 'inside'
              },
              data: res.cnt220102
            },
            {
              name: '宽城',
              type: 'bar',
              color: '#4da8ec',
              label: {
                show: true,
                position: 'inside'
              },
              data: res.cnt220103
            },
            {
              name: '朝阳',
              type: 'bar',
              color: '#3a91d2',
              label: {
                show: true,
                position: 'inside'
              },
              data: res.cnt220104
            },
            {
              name: '二道',
              type: 'bar',
              color: '#005fa6',
              label: {
                show: true,
                position: 'inside'
              },
              data: res.cnt220105
            },
            {
              name: '绿园',
              type: 'bar',
              color: '#315f97',
              label: {
                show: true,
                position: 'inside'
              },
              data: res.cnt220106
            }
          ]
        })
      })
    }
  }
}
</script>
